%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

function varargout = classifier_gui(varargin)
% CLASSIFIER_GUI MATLAB code for classifier_gui.fig
%      CLASSIFIER_GUI, by itself, creates a new CLASSIFIER_GUI or raises the existing
%      singleton*.
%
%      H = CLASSIFIER_GUI returns the handle to a new CLASSIFIER_GUI or the handle to
%      the existing singleton*.
%
%      CLASSIFIER_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CLASSIFIER_GUI.M with the given input arguments.
%
%      CLASSIFIER_GUI('Property','Value',...) creates a new CLASSIFIER_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before classifier_gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to classifier_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help classifier_gui

% Last Modified by GUIDE v2.5 25-Aug-2014 12:47:21

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @classifier_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @classifier_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before classifier_gui is made visible.
function classifier_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to classifier_gui (see VARARGIN)

temp = ones(8,3);
temp = mat2cell( temp , ones( 8 , 1 ) , ones( 1 , 3 ) );
temp{ 8 , 2 } = [];
temp{ 8 , 3 } = [];

set( handles.feat_mult_table , 'data', temp );

% Choose default command line output for classifier_gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes classifier_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = classifier_gui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in Area.
function Area_Callback(hObject, eventdata, handles)
% hObject    handle to Area (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Area


% --- Executes on button press in Perimeter.
function Perimeter_Callback(hObject, eventdata, handles)
% hObject    handle to Perimeter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Perimeter


% --- Executes on button press in Centroid_X.
function Centroid_X_Callback(hObject, eventdata, handles)
% hObject    handle to Centroid_X (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Centroid_X


% --- Executes on button press in Centroid_Y.
function Centroid_Y_Callback(hObject, eventdata, handles)
% hObject    handle to Centroid_Y (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Centroid_Y


% --- Executes on button press in Extent.
function Extent_Callback(hObject, eventdata, handles)
% hObject    handle to Extent (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Extent


% --- Executes on button press in Solidity.
function Solidity_Callback(hObject, eventdata, handles)
% hObject    handle to Solidity (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Solidity


% --- Executes on button press in Orientation.
function Orientation_Callback(hObject, eventdata, handles)
% hObject    handle to Orientation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Orientation


% --- Executes on button press in Major_Axis.
function Major_Axis_Callback(hObject, eventdata, handles)
% hObject    handle to Major_Axis (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Major_Axis


% --- Executes on button press in Minor_Axis.
function Minor_Axis_Callback(hObject, eventdata, handles)
% hObject    handle to Minor_Axis (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Minor_Axis


% --- Executes on button press in Eccentricity.
function Eccentricity_Callback(hObject, eventdata, handles)
% hObject    handle to Eccentricity (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Eccentricity


% --- Executes on button press in Min_Radius.
function Min_Radius_Callback(hObject, eventdata, handles)
% hObject    handle to Min_Radius (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Min_Radius


% --- Executes on button press in Max_Radius.
function Max_Radius_Callback(hObject, eventdata, handles)
% hObject    handle to Max_Radius (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Max_Radius


% --- Executes on button press in Avg_Radius.
function Avg_Radius_Callback(hObject, eventdata, handles)
% hObject    handle to Avg_Radius (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Avg_Radius


% --- Executes on button press in Avg_Intensity.
function Avg_Intensity_Callback(hObject, eventdata, handles)
% hObject    handle to Avg_Intensity (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Avg_Intensity


% --- Executes on button press in Max_Intensity.
function Max_Intensity_Callback(hObject, eventdata, handles)
% hObject    handle to Max_Intensity (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Max_Intensity


% --- Executes on button press in Min_Intensity.
function Min_Intensity_Callback(hObject, eventdata, handles)
% hObject    handle to Min_Intensity (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Min_Intensity


% --- Executes on button press in Brightness_Ratio.
function Brightness_Ratio_Callback(hObject, eventdata, handles)
% hObject    handle to Brightness_Ratio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Brightness_Ratio


% --- Executes on button press in Protrusions.
function Protrusions_Callback(hObject, eventdata, handles)
% hObject    handle to Protrusions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Protrusions


% --- Executes on button press in Prutruding_Area.
function Prutruding_Area_Callback(hObject, eventdata, handles)
% hObject    handle to Prutruding_Area (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Prutruding_Area


% --- Executes on button press in C_Area.
function C_Area_Callback(hObject, eventdata, handles)
% hObject    handle to C_Area (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of C_Area


% --- Executes on button press in C_Perimeter.
function C_Perimeter_Callback(hObject, eventdata, handles)
% hObject    handle to C_Perimeter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of C_Perimeter


% --- Executes on button press in C_Centroid.
function C_Centroid_Callback(hObject, eventdata, handles)
% hObject    handle to C_Centroid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of C_Centroid


% --- Executes on button press in Build_Classifier.
function Build_Classifier_Callback(hObject, eventdata, handles)
% hObject    handle to Build_Classifier (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

find_feats_used;

m_value = get( handles.feat_mult_table, 'data' );
m_value = cat( 1 , cat( 1 , m_value{ : , 1 } ) , cat( 1 , m_value{ : , 2 } ) , cat( 1 , m_value{ : , 3 } ) );

run_settings.NumRuns = str2double( get( handles.NumRuns , 'String' ) );
run_settings.K_Folds = str2double( get( handles.K_Folds , 'String' ) );
run_settings.Mov_Avg_Int = str2double( get( handles.Avg_Interval , 'String' ) );


results = hESC_build_classifier( feats_used , run_settings , m_value );
colnames = { 'Classification Rate' , 'STD' };
rownames = { 'SVM' , 'KNN' , 'Naive Bayes' };

set( handles.uitable1 , 'data', results , 'ColumnName', colnames , 'RowName' , rownames );

%plot classification results

bar( results );
xlabel( 'Classification Methods' )
ylabel( 'Classification Percentage' )
set( gca, 'XTickLabel', rownames );

% Choose default command line output for classifier_gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);



% --- Executes on button press in Save_Classifier.
function Save_Classifier_Callback(hObject, eventdata, handles)
% hObject    handle to Save_Classifier (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

find_feats_used;

m_value = get( handles.feat_mult_table, 'data' );
m_value = cat( 1 , cat( 1 , m_value{ : , 1 } ) , cat( 1 , m_value{ : , 2 } ) , cat( 1 , m_value{ : , 3 } ) );

run_settings.Mov_Avg_Int = str2double( get( handles.Avg_Interval , 'String' ) );

hESC_save_classifier( feats_used , run_settings , m_value);

% Choose default command line output for classifier_gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);




function NumRuns_Callback(hObject, eventdata, handles)
% hObject    handle to NumRuns (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of NumRuns as text
%        str2double(get(hObject,'String')) returns contents of NumRuns as a double


% --- Executes during object creation, after setting all properties.
function NumRuns_CreateFcn(hObject, eventdata, handles)
% hObject    handle to NumRuns (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Avg_Interval_Callback(hObject, eventdata, handles)
% hObject    handle to Avg_Interval (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Avg_Interval as text
%        str2double(get(hObject,'String')) returns contents of Avg_Interval as a double


% --- Executes during object creation, after setting all properties.
function Avg_Interval_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Avg_Interval (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function K_Folds_Callback(hObject, eventdata, handles)
% hObject    handle to K_Folds (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of K_Folds as text
%        str2double(get(hObject,'String')) returns contents of K_Folds as a double


% --- Executes during object creation, after setting all properties.
function K_Folds_CreateFcn(hObject, eventdata, handles)
% hObject    handle to K_Folds (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on key press with focus on edit4 and none of its controls.
function edit4_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when entered data in editable cell(s) in feat_mult_table.
function feat_mult_table_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to feat_mult_table (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in MSD.
function MSD_Callback(hObject, eventdata, handles)
% hObject    handle to MSD (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of MSD


% --- Executes on button press in Total_Distance.
function Total_Distance_Callback(hObject, eventdata, handles)
% hObject    handle to Total_Distance (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of Total_Distance
